package com.tenglong.teacher.ui.adapter.viewholder;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.cuotibao.zhuoxiao.basics.utils.IpUtils;
import com.cuotibao.zhuoxiao.basics.utils.ToastUtils;
import com.cuotibao.zhuoxiao.basics.utils.glide.GlideOptions;
import com.tenglong.teacher.R;
import com.tenglong.teacher.data.resultInfo.ClientUserInfo;
import com.tenglong.teacher.data.resultInfo.DataState;
import com.tenglong.teacher.services.hdx.CreateRequestEntity;
import com.tenglong.teacher.ui.adapter.LoginSelectOrganizationAdapter;
import com.tenglong.teacher.ui.adapter.viewholder.base.BaseViewHolder;
import com.tenglong.teacher.ui.login.LoginSelectOrganizationActivity;

/**
 * @author hdx on 2019/5/30.
 */
public class LoginSelectOrganizationContentViewHolder extends BaseViewHolder<ClientUserInfo> {

    private LoginSelectOrganizationAdapter.LoginSelectOrganizationListener loginSelectOrganizationListener;
    private ImageView icon;
    private TextView name;
    private ClientUserInfo data;
    private View current;
    private boolean isCurrent;
    private boolean isLoginType;
    private RequestOptions options;
    public LoginSelectOrganizationContentViewHolder(View itemView) {
        super(itemView);
        options = GlideOptions.getGlideRequestOptions().
                error(R.drawable.xuexiao).
                placeholder(R.drawable.xuexiao);
        initView();
        setListeners();

    }
    public LoginSelectOrganizationContentViewHolder(View itemView, LoginSelectOrganizationAdapter.LoginSelectOrganizationListener loginSelectOrganizationListener) {
        this(itemView);
        this.loginSelectOrganizationListener = loginSelectOrganizationListener;
    }

    private void initView(){
        icon = itemView.findViewById(R.id.icon);
        name = itemView.findViewById(R.id.name);
        current = itemView.findViewById(R.id.current);
        if (mContext instanceof LoginSelectOrganizationActivity){
            isLoginType = ((LoginSelectOrganizationActivity)mContext).isLoginType();
        }
    }
    private void setListeners(){
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isLoginType){
                    if (loginSelectOrganizationListener != null){
                        loginSelectOrganizationListener.onLoginSelectOrganizationListener(getLayoutPosition(),data);
                    }
                    return;
                }
                if (!isCurrent){
                    if (loginSelectOrganizationListener != null){
                        loginSelectOrganizationListener.onLoginSelectOrganizationListener(getLayoutPosition(),data);
                    }
                } else {
                    ToastUtils.showCenterToast("已经是此机构");
                }
            }
        });
    }

    @Override
    public void setLayoutInfo(ClientUserInfo info) {
        this.data = info;
        isCurrent = info.getId().equals(DataState.getInstance().getUserInfo().getId());
        Glide.with(mContext)
                .load(IpUtils.Ips[0]+info.getCompanyPhotoUrl())
                .apply(options)
                .into(icon);
        name.setText(info.companyName == null?"":info.companyName);
        current.setVisibility(isLoginType?View.GONE:(isCurrent?View.VISIBLE:View.GONE));
    }
}
